define(function(require, exports, module) {
	
	var fit = require("fitdatagrid");
	
	var dateRender = function(h, params) {
		var value = params.row[params.column.key];
        return h('span', {}, value ? value.substring(0, 19) : value);
    }
	
	module.exports = function() {
		var table = {};
		return {
			data: {
				search: {
					placeholder: $.i18n.prop("info.search.placeholder"),
					button:$.i18n.prop("info.search"),
					value:""
				},
				perms : {
					add:false,
					edit:false,
					remove:false,
					perm:false
				}, 
				button : {
					disabled:true,
					text: {
						add:$.i18n.prop("info.add"),
						edit:$.i18n.prop("info.edit"),
						remove:$.i18n.prop("info.remove"),
						perm:$.i18n.prop("sys.role.manager.perm")
					}
				},
				paging:{
					height:200,
					data:[],
					columns:[
					    {
					    	type:'selection',
					     	width: 50,
					    	align: 'center',
					    	fixed: 'left'
					    }, {
				        	key:'id', 
				        	title:$.i18n.prop("sys.role.manager.id"), 
				        	width:200
				        }, {
				        	key:'name', 
				        	title:$.i18n.prop("sys.role.manager.name"), 
				        	width:200
				        }, {
				        	type: "html",
				        	key:'memo',
				        	title:$.i18n.prop("sys.role.manager.memo"),
				        	minWidth: 300
				        }, {
				        	key:'createtime', 
				        	title:$.i18n.prop("sys.createtime"), 
				        	width: 150,
				        	align: 'center',
				        	formatter:dateRender
				        }, {
				        	key:'updatetime', 
				        	title:$.i18n.prop("sys.updatetime"), 
				        	width: 150,
				        	align: 'center',
				        	formatter:dateRender
				        }
					],
					selections:[],
					loading:false,
					page:1,
					pageSize:10,
					total:0
				}
			},
			methods: {
				init: function(data) {
					var me = this;
					var params = {
						add:"sys:role:manager:add",
						edit:"sys:role:manager:edit",
						remove:"sys:role:manager:remove",
						perm:"sys:role:manager:perm"
					};
					//获取权限
					$.perms(params, function(isOk, data) {
						if(isOk) {
							$.extend(me.perms, data);
						}
						//初始化角色列表
						fit.fit($(me.$el).find(".layout-main"), me.paging);
						me.loadDataGrid(function() {
							this.$nextTick(function() {
		                    	this.loading("close");
		                    });
						});
					});
					return false;
				},
				loadDataGrid:function(finish) {
					var me = this;
					if(!me.paging.loading) {
						me.paging.loading = true;
						me.onSelectionChange([]);
						$.post("/api/role/datagrid", {
							pageNumber: me.paging.page,
							pageSize: me.paging.pageSize,
							searchText: me.search.value
						}, function(data) {
							me.paging.total = data.total;
							me.paging.data = data.rows;
							me.paging.loading = false;
							if($.isFunction(finish)) {
								finish.call(me);
							} 
						}, "json");
					}
				},
				onSearch:function(value) {
					this.search.value = value;
					this.paging.page = 1;
					this.loadDataGrid();
				},
				onSelectionChange:function(rows) {
					this.paging.selections = rows;
					this.button.disabled = rows.length == 0;
				},
				onPageChange:function(page) {
					this.paging.page = page;
					this.loadDataGrid();
				},
				onAddClicked: function() {
					var me = this;
					var extraParams = {
						refresh: function() {
							me.loadDataGrid();
						}
					};
					$.window({
						router: "sys/role/add",
						shade: 0.3
					}, extraParams);
				},
				onEditClicked: function() {
					var me = this;
					var rows = this.paging.selections;
					var extraParams = {
						row:rows[0],
						update:function(result) {
							$(me.paging.data).each(function() {
								if(this.id == result.id) {
									$.extend(this, result);
									me.onSelectionChange([]);
									return false;
								}
							});
						}
					};
					$.window({
						router: "sys/role/edit",
						shade: 0.3,
					}, extraParams);
				},
				onRemoveClicked: function() {
					var me = this;
					$.confirm($.i18n.prop("sys.remove.confirm"), function(isOk) {
						if(isOk) {
							var _this = this;
							var rows = me.paging.selections;
							var ids = [];
							$(rows).each(function() {
								ids.push(this.id);
							});
							$.post("/api/role/remove", {ids:ids}, function(data) {
								if(data.success) {
									me.loadDataGrid();
								}
								_this.remove();
							}, "json");
						} else {
							this.remove();
						}
					}, true);
				},
				onAllocClicked: function() {
					var rows = this.paging.selections;
					$.window({
						router: "sys/role/perm",
						shade: 0.3
					}, rows[0]);
				},
				destroy: function() {
				}
			}
		}
	}
});